<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable />
        </el-form-item>
        <el-form-item prop="cardType">
          <DictSelect dict="data_card_type" v-model="queryParams.cardType" placeholder="卡类型" clearable />
        </el-form-item>
        <el-form-item prop="status">
          <DictSelect dict="data_card_status" v-model="queryParams.status" placeholder="状态" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:dataCard:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="tool-handle">
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['biz:dataCard:add']">新增数据卡</el-button>
      </div>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #status="{ row }">
          <dict-tag :options="data_card_status" :value="row.status" />
        </template>
        <template #type="{ row }">
          <dict-tag :options="data_card_type" :value="row.cardType" />
        </template>
        <template #handle="{ row }">
          <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:dataCard:query']">详情</el-button>
          <el-button
            link
            type="primary"
            :disabled="row.status == 3 || row.cardType == 2"
            @click="handleRenewal(row)"
            v-hasPermi="['biz:dataCard:edit']"
            >延期</el-button
          >
          <el-button link type="primary" :disabled="row.status == 3" @click="handleUpdate(row)" v-hasPermi="['biz:dataCard:edit']">修改</el-button>
          <el-button link type="danger" :disabled="row.status == 3 || row.deviceId" @click="handleAbolish(row)" v-hasPermi="['biz:dataCard:edit']"
            >作废</el-button
          >
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>
    <AddCard ref="refAddCard" @success="handleQuery"></AddCard>
    <EditCard ref="refEditCard" @success="getList"></EditCard>
    <RenewalCard ref="refRenewalCard" @success="getList"></RenewalCard>
  </div>
</template>

<script setup name="StationDataCard" lang="ts">
import { listDataCard, abolishDataCard } from '@/api/station/dataCard/index';
import { BizStationDataCardVo } from '@/api/station/dataCard/types';
import { getTableConfig } from './columns';
import AddCard from './common/add-card/index.vue';
import EditCard from './common/edit-card/index.vue';
import RenewalCard from './common/renewal-card/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { data_card_type, data_card_status } = toRefs<any>(proxy?.useDict('data_card_type', 'data_card_status'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const refAddCard = ref();
const refEditCard = ref();
const refRenewalCard = ref();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  stationName: '',
  status: '',
  regionCode: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDataCard(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = () => {
  refAddCard?.value.open();
};

/** 作废 */
const handleAbolish = async (row?: BizStationDataCardVo) => {
  const _ids = row?.cardId || '';
  await proxy?.$modal.confirm('是否确认作废卡号【"' + row?.cardNo + '"】的数据卡？');
  loading.value = true;
  await abolishDataCard(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('作废成功');
};
/** 延期 */
const handleRenewal = async (row?: BizStationDataCardVo) => {
  refRenewalCard?.value.openByInfo(row);
};
/** 修改 */
const handleUpdate = async (row?: BizStationDataCardVo) => {
  refEditCard?.value.openByInfo(row);
};
/** 详情 */
const handleDetail = async (row?: BizStationDataCardVo) => {
  proxy?.$tab.openPage('/station/dataCardDetail', '', { id: row?.cardId });
};

onMounted(() => {
  getList();
});
</script>
